package com.sdtele.tta.operation;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sdtele.tta.dac.Dac;
import com.sdtele.tta.models.JsonRequest;
import com.sdtele.tta.models.JsonResponse;
import com.sdtele.tta.models.Task;
import com.sdtele.tta.models.TaskOperation;
import com.sdtele.tta.models.TaskOut;
import com.sdtele.tta.models.User;

public class OperationGetTaskList extends Operation {

	public JsonResponse handle(JsonRequest jsonRequest) {
		System.out.println("OperationLogin:req=" + jsonRequest);
		JsonResponse jres = new JsonResponse();
		User user = (User) request.getSession().getAttribute("user");
		if (user != null)

		{	String  action="null";
			
			if(jsonRequest.getJsonObject().get("action") !=null)
			{  action = jsonRequest.getJsonObject().get("action").getAsString();}
			
				System.out.println(action);
			
			// List<Task> taskListAll = Dac.getInstance().getTask(0);
			List<Task> taskListAll = Dac.getInstance().getTask(user.getId());

			// for (Task task:taskListAll)
			// System.out.println(task);

			List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
			List<Map<String, Object>> undonemapList = new ArrayList<Map<String, Object>>();
			List<Map<String, Object>> historymapList = new ArrayList<Map<String, Object>>();
			List<Map<String, Object>> readmapList = new ArrayList<Map<String, Object>>();
			
			if (taskListAll != null) {
				for (Task task : taskListAll)

				{
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("task", task.ToTaskOut());
					User owner = Dac.getInstance().getUser(task.getOwnerId());
					map.put("owner", owner);
					// System.out.println(task.getId()+" "+user.getId());
					TaskOperation taskOp = Dac.getInstance().getOperation(task.getId(), user.getId());

					// 测试用例 userid默认为0
					// 增加是否分阅人标签reader 0：表示非分阅人 1：表示为分阅人
					int reader = -1;
					if (taskOp.getOperation() == 2)

						reader = 1;
					else
						reader = 0;
					map.put("reader", reader);
					mapList.add(map);
				}
			}
			
			if(action.equals("u"))
			{
				for(Map<String, Object> map :mapList)
				{
					if((int)map.get("reader")!=1)
					{
						TaskOut taskOut = (TaskOut)map.get("task");
						if(taskOut.getCompletion() <100)
						{
							undonemapList.add(map);
						}
					}
				}
				
				jres.setStatus(0);
				jres.setMessage("用户:未完成任务列表");
				jres.setEntity(undonemapList);
				return jres;
			}
			
			
			if(action.equals("h"))
			{
				for(Map<String, Object> map :mapList)
				{
					
						TaskOut taskOut = (TaskOut)map.get("task");
						if(taskOut.getCompletion() >=100)
						{
							historymapList.add(map);
						}
					
				}
				
				jres.setStatus(0);
				jres.setMessage("用户:历史任务列表");
				jres.setEntity(historymapList);
				return jres;
			}
			
			
			if(action.equals("r"))
			{
				for(Map<String, Object> map :mapList)
				{
					if((int)map.get("reader")==1)
					{
						
						readmapList.add(map);
						
					}
				}
				
				
				
				jres.setStatus(0);
				jres.setMessage("用户:分阅任务列表");
				jres.setEntity(readmapList);
				return jres;
			}
			
			
			jres.setStatus(0);
			jres.setMessage("用户:全部任务列表");
			jres.setEntity(mapList);
			
			
		} else {
			jres.setStatus(1);
			jres.setMessage("用户登录失效,请重新登录");
		}

		return jres;

	}

}